package fr.dauphine.td3.ctl;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.dauphine.td3.bdd.EmployeDAO;
import fr.dauphine.td3.emp.Employe;

@Controller
@RequestMapping("user/photo")
public class PhotoController {
	
	@Autowired
	private EmployeDAO employeDAO;
	
	@RequestMapping(method=RequestMethod.GET) 
	public void affichage(@RequestParam("id") String id, HttpServletResponse response) {
		
		if (id != null && id != "" && id != "-1") {
		
			Employe e = employeDAO.get(id);
			
			response.setHeader("Expires", "0");
			response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");
			
			response.setContentType("image/jpeg");
			response.setContentLength(e.getPhoto().length);
			
			try {
				ServletOutputStream o = response.getOutputStream();
				o.write(e.getPhoto());
				o.flush();
				
			} catch (IOException e1) {
				System.out.println("Erreur ecriture photo dans flux http");
				e1.printStackTrace();
			}
		
		}
		
	}

}
